//
//  ICMembersListlSearchController.m
//  ICome
//
//  Created by Administrator on 2016/12/29.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICMembersListlSearchController.h"
#import "ICContactListCell.h"
#import "UIImage+AvatarGeneration.h"

@interface ICMembersListlSearchController ()

@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) UILabel * emptyStyleLable;

@end

@implementation ICMembersListlSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}


- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:animated];
}

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55.0f * Level;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *res = @"cell";
    ICContactListCell *cell = [tableView dequeueReusableCellWithIdentifier:res];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell) {
        cell = [[ICContactListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:res];
    }
    ICUser *model = [self.dataList objectAtIndex:indexPath.row];
    cell.titleLabel.text = model.eName;
    NSString *URL = [ICTools getMinImageURL:(model.photoId)];
    [UIImage addAvatariamgeView:cell.headImageView withUrlStr:URL witheId:model.eId withName:model.eName];
//    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:URL] placeholderImage:[UIImage imageNamed:@"App_personal_headimg"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICUser *user = self.dataList[indexPath.row];
    NSString * name = [NSString stringWithFormat:@"%@\u2005",user.nName ==nil ?user.eName:user.nName];
    if ([self.delegate respondsToSelector:@selector(detailSearch:)]) {
        [self.delegate detailSearch:@{user.eId:name}];
    }
//    [self dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark - UISearchResultsUpdating

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchTxt = searchController.searchBar.text;
    if (searchTxt) {
        [self searchUsers:searchTxt];
    }
}

- (void)searchUsers:(NSString *)text
{
    [self.emptyStyleLable removeFromSuperview];
    [self.dataList removeAllObjects];
    __weak typeof(self) weakSelf = self;
    [ICMessageDatabase searchUsersWithGroupId:self.gId searchTxt:text finish:^(NSArray *array, NSError *error) {
        if (array.count>0) {
            [weakSelf.dataList addObjectsFromArray:array];
        } else {
            [weakSelf.tableView addSubview:weakSelf.emptyStyleLable];
        }
        [weakSelf.tableView reloadData];
    }];
}

#pragma mark - private

- (void)setupUI
{
    self.tableView.backgroundColor = BACKGROUNDCOLOR;
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
}

#pragma mark - Getter

- (UILabel *)emptyStyleLable
{
    if (!_emptyStyleLable) {
        _emptyStyleLable       = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, App_Frame_Width, 30)];
        _emptyStyleLable.font  = [UIFont systemFontOfSize:17];
        _emptyStyleLable.backgroundColor = [UIColor clearColor];
        _emptyStyleLable.textAlignment = NSTextAlignmentCenter;
        _emptyStyleLable.textColor     = ICRGB(0x9a9a9a);
        _emptyStyleLable.text = @"暂无搜索结果";
        [self.view addSubview:_emptyStyleLable];
    }
    return _emptyStyleLable;
}

-(NSMutableArray *)dataList
{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}




@end
